﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace Marmat.Forms
{
    internal class RenderHelper
    {
        internal static void RenderBackgroundInternal(Graphics g, Rectangle rect, Color baseColor, Color borderColor, Color innerBorderColor, RoundStyle style, bool drawBorder, bool drawGlass, LinearGradientMode mode)
        {
            RenderBackgroundInternal(g, rect, baseColor, borderColor, innerBorderColor, style, 8, drawBorder, drawGlass, mode);
        }

        internal static void RenderBackgroundInternal(Graphics g, Rectangle rect, Color baseColor, Color borderColor, Color innerBorderColor, RoundStyle style, int roundWidth, bool drawBorder, bool drawGlass, LinearGradientMode mode)
        {
            RenderBackgroundInternal(g, rect, baseColor, borderColor, innerBorderColor, style, 8, 0.45f, drawBorder, drawGlass, mode);
        }

        internal static void RenderBackgroundInternal(Graphics g, Rectangle rect, Color baseColor, Color borderColor, Color innerBorderColor, RoundStyle style, int roundWidth, float basePosition, bool drawBorder, bool drawGlass, LinearGradientMode mode)
        {
            if (drawBorder)
            {
                rect.Width -= 1;
                rect.Height -= 1;
            }

            if (rect.Width == 0 || rect.Height == 0)
            {
                return;
            }

            using (var brush = new LinearGradientBrush(rect, Color.Transparent, Color.Transparent, mode))
            {
                var colors = new Color[4];
                colors[0] = GetColor(baseColor, 0, 35, 24, 9);
                colors[1] = GetColor(baseColor, 0, 13, 8, 3);
                colors[2] = baseColor;
                colors[3] = GetColor(baseColor, 0, 35, 24, 9);
                var blend = new ColorBlend();
                blend.Positions = new float[] { 0.0f, basePosition, basePosition + 0.05f, 1.0f };
                blend.Colors = colors;
                brush.InterpolationColors = blend;
                if (style != RoundStyle.None)
                {
                    using (var path = GraphicsPathHelper.CreatePath(rect, roundWidth, style, false))
                    {
                        g.FillPath(brush, path);
                    }

                    if (baseColor.A > 80)
                    {
                        var rectTop = rect;
                        if (mode == LinearGradientMode.Vertical)
                        {
                            rectTop.Height = (int)Math.Round(Math.Truncate(rectTop.Height * basePosition));
                        }
                        else
                        {
                            rectTop.Width = (int)Math.Round(Math.Truncate(rect.Width * basePosition));
                        }

                        using (var pathTop = GraphicsPathHelper.CreatePath(rectTop, roundWidth, RoundStyle.Top, false))
                        {
                            using (var brushAlpha = new SolidBrush(Color.FromArgb(128, 255, 255, 255)))
                            {
                                g.FillPath(brushAlpha, pathTop);
                            }
                        }
                    }

                    if (drawGlass)
                    {
                        RectangleF glassRect = rect;
                        if (mode == LinearGradientMode.Vertical)
                        {
                            glassRect.Y = rect.Y + rect.Height * basePosition;
                            glassRect.Height = (rect.Height - rect.Height * basePosition) * 2f;
                        }
                        else
                        {
                            glassRect.X = rect.X + rect.Width * basePosition;
                            glassRect.Width = (rect.Width - rect.Width * basePosition) * 2f;
                        }

                        ControlPaintEx.DrawGlass(g, glassRect, 170, 0);
                    }

                    if (drawBorder)
                    {
                        using (var path = GraphicsPathHelper.CreatePath(rect, roundWidth, style, false))
                        {
                            using (var pen = new Pen(borderColor))
                            {
                                g.DrawPath(pen, path);
                            }
                        }

                        rect.Inflate(-1, -1);
                        using (var path = GraphicsPathHelper.CreatePath(rect, roundWidth, style, false))
                        {
                            using (var pen = new Pen(innerBorderColor))
                            {
                                g.DrawPath(pen, path);
                            }
                        }
                    }
                }
                else
                {
                    g.FillRectangle(brush, rect);
                    if (baseColor.A > 80)
                    {
                        var rectTop = rect;
                        if (mode == LinearGradientMode.Vertical)
                        {
                            rectTop.Height = (int)Math.Round(Math.Truncate(rectTop.Height * basePosition));
                        }
                        else
                        {
                            rectTop.Width = (int)Math.Round(Math.Truncate(rect.Width * basePosition));
                        }

                        using (var brushAlpha = new SolidBrush(Color.FromArgb(128, 255, 255, 255)))
                        {
                            g.FillRectangle(brushAlpha, rectTop);
                        }
                    }

                    if (drawGlass)
                    {
                        RectangleF glassRect = rect;
                        if (mode == LinearGradientMode.Vertical)
                        {
                            glassRect.Y = rect.Y + rect.Height * basePosition;
                            glassRect.Height = (rect.Height - rect.Height * basePosition) * 2f;
                        }
                        else
                        {
                            glassRect.X = rect.X + rect.Width * basePosition;
                            glassRect.Width = (rect.Width - rect.Width * basePosition) * 2f;
                        }

                        ControlPaintEx.DrawGlass(g, glassRect, 200, 0);
                    }

                    if (drawBorder)
                    {
                        using (var pen = new Pen(borderColor))
                        {
                            g.DrawRectangle(pen, rect);
                        }

                        rect.Inflate(-1, -1);
                        using (var pen = new Pen(innerBorderColor))
                        {
                            g.DrawRectangle(pen, rect);
                        }
                    }
                }
            }
        }

        internal static void RenderArrowInternal(Graphics g, Rectangle dropDownRect, ArrowDirection direction, Brush brush)
        {
            var point = new Point(dropDownRect.Left + dropDownRect.Width / 2, dropDownRect.Top + dropDownRect.Height / 2);
            Point[] points = null;
            switch (direction)
            {
                case ArrowDirection.Left:
                    {
                        points = new Point[] { new Point(point.X + 1, point.Y - 4), new Point(point.X + 1, point.Y + 4), new Point(point.X - 2, point.Y) };
                        break;
                    }

                case ArrowDirection.Up:
                    {
                        points = new Point[] { new Point(point.X - 4, point.Y + 1), new Point(point.X + 4, point.Y + 1), new Point(point.X, point.Y - 2) };
                        break;
                    }

                case ArrowDirection.Right:
                    {
                        points = new Point[] { new Point(point.X - 2, point.Y - 4), new Point(point.X - 2, point.Y + 4), new Point(point.X + 1, point.Y) };
                        break;
                    }

                default:
                    {
                        points = new Point[] { new Point(point.X - 4, point.Y - 1), new Point(point.X + 4, point.Y - 1), new Point(point.X, point.Y + 2) };
                        break;
                    }
            }

            g.FillPolygon(brush, points);
        }

        internal static void RenderAlphaImage(Graphics g, Image image, Rectangle imageRect, float alpha)
        {
            using (var imageAttributes = new ImageAttributes())
            {
                var colorMap = new ColorMap();
                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
                var remapTable = new[] { colorMap };
                imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
                var colorMatrixElements = new[] { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, alpha, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } };
                var wmColorMatrix = new ColorMatrix(colorMatrixElements);
                imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                g.DrawImage(image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
            }
        }

        internal static void RenderGrid(Graphics g, Rectangle rect, Size PixelsBetweenDots, Color OuterColor)
        {
            int Win32Corlor = ColorTranslator.ToWin32(OuterColor);
            var hdc = g.GetHdc();
            int x = rect.X;
            while (x <= rect.Right)
            {
                int y = rect.Y;
                while (y <= rect.Bottom)
                {
                    SetPixel(hdc, x, y, Win32Corlor);
                    y += PixelsBetweenDots.Height;
                }

                x += PixelsBetweenDots.Width;
            }

            g.ReleaseHdc(hdc);
        }

        internal static Color GetColor(Color colorBase, int a, int r, int g, int b)
        {
            int a0 = colorBase.A;
            int r0 = colorBase.R;
            int g0 = colorBase.G;
            int b0 = colorBase.B;
            if (a + a0 > 255)
            {
                a = 255;
            }
            else
            {
                a = Math.Max(0, a + a0);
            }

            if (r + r0 > 255)
            {
                r = 255;
            }
            else
            {
                r = Math.Max(0, r + r0);
            }

            if (g + g0 > 255)
            {
                g = 255;
            }
            else
            {
                g = Math.Max(0, g + g0);
            }

            if (b + b0 > 255)
            {
                b = 255;
            }
            else
            {
                b = Math.Max(0, b + b0);
            }

            return Color.FromArgb(a, r, g, b);
        }

        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        private static extern uint SetPixel(IntPtr hdc, int X, int Y, int crColor);
    }
}